public class LinkedList2 {
    static class ListNode{
        public int val;
        public ListNode next;

        public  ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    //判断回文
    public boolean chkPalindrome(){
        //找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast !=null && fast.next != null ) {
         fast = fast.next.next;
         slow = slow.next;
        }
        //反转中间节点后的节点

        ListNode cur =slow.next;
        while (cur != null){
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }

        //head跟slow同时走，判断回文(注意偶数情况)
        while (head != slow){
            if (head.val != slow.val){
                return false;
            }
            //判断偶数情况
            if (head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }




    //返回倒数第k个节点
    public int kthToLast(int k){
        //判断k的合法性
        if(k < 0 || head == null){
            return -1;
        }
        //fast先走k-1步
        ListNode fast = head;
        int count = 0;
        while (count != k-1){
            fast = fast.next;
            if(fast == null){
                return -1;
            }
            count++;
        }
        //找到倒数第k节点
        ListNode slow = head;
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    //找到中间节点
    public ListNode middleNode(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }


    //反转单链表
    public ListNode reverseList(){
        //此链表为空
        if(head == null){
            return null;
        }//只有一个节点
        if (head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;

        while (cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }


    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }

        ListNode cur = head;
        while (cur.next !=null){
            cur = cur.next;
        }
        cur.next = node;
    }


    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display(ListNode NewHead){
        ListNode cur = NewHead;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

}
